<template>
	<view class="preview" v-if="currentData._id">
		<swiper circular :current="index" @change="swiperChange">
			<swiper-item v-for="(item,index) in classList" :key="item._id">
				<image v-if="indexList.includes(index)" @click="maskChange" :src="item.picurl" mode="aspectFill"></image>
			</swiper-item>
		</swiper>
	
		<!-- 遮罩 -->
		<view class="mask" v-show="maskState">
			<view class="goBack" :style="{top:getBackTop() + 'px'}" @click="onClickBack">
				<uni-icons type="back" size="18" color="#fff"></uni-icons>
			</view>
			<view class="count">{{index + 1}} / {{classList.length}}</view>
			<view class="time">
				<uni-dateformat :date="new Date()" format="hh:mm"></uni-dateformat>
			</view>
			<view class="date">
				<uni-dateformat :date="new Date()" format="MM月dd日"></uni-dateformat>
			</view>
			<view class="footer">
				<view class="box" @click="onDetails">
					<uni-icons type="info" size="28"></uni-icons>
					<view class="text">详情</view>
				</view>
				<view class="box" @click="onClickRate">
					<uni-icons type="star" size="28"></uni-icons>
					<view class="text">{{currentData?.score}}分</view>
				</view>
				<view class="box" @click="onDownload">
					<uni-icons type="download" size="25"></uni-icons>
					<view class="text">下载</view>
				</view>
			</view>
		</view>
		<!-- 详情弹窗 -->
		<uni-popup ref="details" type="bottom">
			<view class="details">
				<view class="top">
					<view></view>
					<view class="title">壁纸信息</view>
					<view class="clise">
						<uni-icons @click="details.close()" type="closeempty" size="16"></uni-icons>
					</view>
				</view>
				<scroll-view class="main" scroll-y>
					<view class="box">
						<view class="boxTitle">壁纸ID：</view>
						<text selectable class="text">{{currentData._id}}</text>
					</view>
					<!-- <view class="box">
						<view class="boxTitle">分类：</view>
						<text class="text">明星美女</text>
					</view> -->
					<view class="box">
						<view class="boxTitle">发布者：</view>
						<text class="text">{{currentData.nickname}}</text>
					</view>
					<view class="box">
						<view class="boxTitle">评分：</view>
						<view class="text rateDetails">
							<uni-rate :size="16" readonly :value="currentData.score"/>
							<text>{{currentData.score}}分</text>
						</view>
					</view>
					<view class="box">
						<view class="boxTitle">摘要：</view>
						<view class="text">{{currentData.description}}</view>
					</view>
					<view class="box">
						<view class="boxTitle">标签：</view>
						<view class="text">
							<text class="label" v-for="i in currentData.tabs" :key="i">{{i}}</text>
						</view>
					</view>
					
					<view class="mzsm">
						声明：文图片来自用户投稿，非商业用途，用于免费学习交流，如侵犯了您的权益请复制壁纸ID联系邮箱513894357@qq.com，维护您的权益。
					</view>
					<view class=".safe-area-inset-bottom"></view>
				</scroll-view>
			</view>
		</uni-popup>
	
		<!-- 评分弹框 -->
		<uni-popup ref="rate" touchable allowHalf :is-mask-click="false">
			<view class="rate">
				<view class="title">
					<view class="left">dd</view>
					<view  class="text" >{{isRate ? '已评过分了~' : '壁纸评分'}}</view>
					<uni-icons color="#a7a7a7" @click="onUnRate" type="closeempty" size="16"></uni-icons>
				</view>
				<view class="divide">
					<uni-rate size="30" :touchable="false" v-model="rateDivide" :disabled="isRate" disabled-color="#FFCA3E" />
					<view class="text" :class="{selected: rateDivide>0}">{{rateDivide}}分</view>
				</view>
				<view class="submit" :class="{selected: rateDivide>0 && !isRate}" @click="setupScore">确定评分</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import {onReady,onLoad,onShareAppMessage,onShareTimeline,onUnload} from '@dcloudio/uni-app';
import {getStatusBarHeight,getBackTop} from '@/utils/system.js';
import { usePreviewStore } from '@/store/preview.js';
import {setupScoreApi,downloadWallApi,detailWallApi} from '@/api/apis.js';



	onShareAppMessage(e => {
		return {
			title: "好看的壁纸推荐",
			path:'/pages/preview/preview?type=share&_id='+_id.value
		}
	})
	onShareTimeline(() => {
		return{
			title:"好看的壁纸推荐",
		}
	})

	// 获取pinia
	const previewStore = usePreviewStore()
	onUnload(()=>{
		previewStore.setClassList([])
	})
	// console.log(previewStore);
	//存储图片
	const classList = ref([])
	//获取图片id
	const _id = ref('')
	//根据id获得图片数组的下标
	let index = ref(0)
	//节流处理
	const indexList = ref([])
	//当前图片信息
	const currentData = ref({})
	
	//处理图片路径把图片后缀改了达到让图片清晰度变大的效果
	classList.value = previewStore.classList.map(item => {
		return {
			...item,
			picurl: item.smallPicurl.replace("_small.webp",".jpg")
		}
	})

	onLoad ( async e => {
		if(e.type == 'share'){
			const res = await detailWallApi({id:e._id})
			classList.value = res.map(item => {
				return {
					...item,
					picurl: item.smallPicurl.replace("_small.webp",".jpg")
				}
			})
			
		}
		_id.value = e._id
		//根据id获得图片数组的下标
		 index.value = classList.value.findIndex(item => item._id === _id.value)
		 //把下标放入节流的下标数组里
		 indexList.value.push(
		 index.value <=0 ? classList.value.length -1 : index.value - 1,
		 index.value,
		 index.value >= classList.value.length - 1 ? 0 : index.value + 1,
		 )
		 indexList.value = [...new Set(indexList.value)]
		 //存储当前图片信息
		 currentData.value = classList.value[index.value]
	})
	
	//滑动图片的监听事件
	const swiperChange = e => {
		index.value = e.detail.current
		//把下标放入节流的下标数组里
		indexList.value.push(
		index.value <=0 ? classList.value.length -1 : index.value - 1,
		index.value,
		index.value >= classList.value.length - 1 ? 0 : index.value + 1,
		)
		indexList.value = [...new Set(indexList.value)]
		//存储当前图片信息
		currentData.value = classList.value[index.value]
	}
	
	//点击图片隐藏遮罩层
	const maskState = ref(true)
	const maskChange = () => {
		maskState.value = !maskState.value
	}
	// 详情弹窗
	const details = ref(null)
	const onDetails = () => details.value?.open()

	//评分弹框
	const rate = ref(null)
	//是否评分过
	const isRate = ref(false)
	//评分数
	const rateDivide = ref(0)
	const onClickRate = () => {
		if(classList.value[index.value].userScore >= 0) {
			isRate.value = true
			rateDivide.value = currentData.value.userScore
		}
		rate.value.open()
	}
	
	//关闭弹窗评分
	const onUnRate = () => {
		rate.value.close()
		setTimeout(()=>{
			rateDivide.value = 0
			isRate.value = false
		},100)
		
	}
	
	//评分请求
	const setupScore = async () => {
		if(isRate.value || rateDivide.value === 0) return
		uni.showLoading({
			title:"正在加载..."
		})
		const {classid,_id:wallId} = currentData.value
		const res = await setupScoreApi({
			classid,
			wallId,
			userScore: rateDivide.value
		})
		uni.hideLoading()
		if(res.id){
			uni.showToast({
				title:"评分成功",
				icon:'none'
			})
		}
		classList.value[index.value].userScore = rateDivide.value
		previewStore.setClassList(classList.value)
		rateDivide.value = 0
		rate.value.close()
	}
	
	//返回上一页
	const onClickBack = () => {
		uni.navigateBack({
			fail() {
				uni.reLaunch({
					url:'/pages/index/index'
				})
			}
		})
	}

	//下载
	const onDownload = async () => {
		// #ifdef H5
			uni.showModal({
				content:"请长按图片保存",
				showCancel:false
			})
		// #endif
		
		// #ifndef H5
		uni.showLoading({
			title:"正在下载..."
		})
		//下载壁纸的接口用于我的页面里的下载历史查看功能
		try{
			const {classid,_id:wallId} = currentData.value
			const res = await downloadWallApi({
				classid,
				wallId
			})
		}catch(err){
			uni.hideLoading()
			return
		}
		
		uni.getImageInfo({
			src:currentData.value.picurl,
			success(res) {
				uni.saveImageToPhotosAlbum({
					filePath:res.path,
					success(res) {
						uni.showToast({
							title:"保存成功",
							icon:'none'
						})
					},
					fail(res) {
						if(res.errMsg == 'saveImageToPhotosAlbum:fail user cancel'){
							uni.showToast({
								title:"取消保存",
								icon:'none'
							})
							return
						}
						uni.showModal({
							title:"提示",
							content:"需要授权保存相册",
							success(res) {
								if(res.confirm){
									uni.openSetting({
										success(setting) {
											if(setting.authSetting['scope.writePhotosAlbum']){
												uni.showToast({
													title:"获取权限成功",
													icon:'none'
												})
											}else{
												uni.showToast({
													title:"获取权限失败",
													icon:'none'
												})
											}
										}
									})
								}
							}
						})
					}
				})
			}
		})
		
		// #endif
	}

</script>

<style lang="scss" scoped>
.preview{
	position: relative;
	width: 100%;
	height: 100vh;
	swiper{
		width: 100%;
		height: 100%;
		image{
			width: 100%;
			height: 100vh;
		}
	}
	.mask{
		.goBack{
			position: absolute;
			top: 0;
			left: 70rpx;
			width: 65rpx;
			height: 65rpx; 
			display: flex;
			border-radius: 50%;
			justify-content: center;
			align-items: center;
			background: rgba(0, 0, 0, 0.5);
		}
		&>view{
			position: absolute;
			left: 50%;
			transform: translateX(-50%);
			color: #fff;
		}
		.count{
			top: 10vh;
			//上面两行和下面注释都可以实现居中
			// left: 0;
			// right: 0;
			// margin: auto;
			// width: fit-content; 这个意思是宽度和内容一样大
			backdrop-filter: blur(10rpx);
			background-color: rgba(0, 0, 0, 0.3);
			border-radius: 40rpx;
			font-size: 28rpx;
			padding: 8rpx 28rpx;
		}
		.time{
			font-size: 140rpx;
			top: calc(10vh + 80rpx);
			font-weight: 100;
			line-height: 1em;
			text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
		}
		.date{
			font-size: 34rpx;
			top: calc(10vh + 230rpx);
			text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
		}
		.footer{
			background-color: rgba(255, 255, 255, 0.8);
			bottom: 10vh;
			width: 65vw;
			height: 120rpx;
			border-radius: 60rpx;
			color: #000;
			display: flex;
			justify-content: space-around;
			align-items: center;
			box-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
			backdrop-filter: blur(20rpx);
			.box{
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				padding: 2rpx 12rpx;
				.text{
					font-size: 26rpx;
					color: text-font-colot-2;
				}
			}
		}
	}
	.details{
		background: #fff;
		border-radius: 30rpx 30rpx 0 0;
		padding: 0 20rpx;
		.top{
			padding: 20rpx;
			padding-bottom: 0;
			display: flex;
			justify-content: space-between;
			.title{
				color:$text-font-colot-2;
				font-size: 26rpx;
			}
			.close{
				padding: 10rpx;
			}
		}
		.main{
			.mzsm{
				overflow: hidden;
				margin: 0 30rpx 40rpx;
				background-color: $border-color-light;
				color: #a7a7a7;
				padding: 20rpx;
				border-radius: 10rpx;
				line-height: 1.5em;
				
			}
			width: 100%;
			max-height: 60vh;
			.box{
				display: flex;
				width: 100%;
				padding: 12px 0;
				font-size: 32rpx;
				line-height: 1.7em;
				.boxTitle{
					width: 140rpx;
					font-size: 30rpx;
					text-align: right;
					color: #a7a7a7;
				}
				.text{
					flex: 1;
					width: 0;
					.label{
						margin-left: 8rpx;
						font-size: 22rpx;
						height: 30rpx;
						color: $brand-theme-color;
						padding: 5rpx 25rpx;
						border-radius: 40rpx;
						border: 1px solid $brand-theme-color;
					}
				}
				.rateDetails{
					display: flex;
					align-items: center;
					text{
						margin-left: 2rpx;
						color: #FFCA3E;
					}
				}
				
			}
		}
	}
	.rate{
		width: 550rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding: 20rpx 50rpx;
		border-radius: 30rpx;
		background: #fff;
		.title{
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			color: #a7a7a7;
			.left{
				opacity: 0;
			}
		}
		
		.divide{
			padding: 50rpx 0;
			display: flex;
			align-items: center;
			.text{
				margin-left: 20rpx;
				color: $text-font-colot-2;
				&.selected{
					color: #FFCA3E;
				}
			}
		}
		.submit{
			border: 1px solid $text-font-color-3;
			color: $text-font-color-3;
			padding: 15rpx 20rpx;
			border-radius: 15rpx;
			margin-bottom: 20rpx;
			&.selected{
				border: 1px solid $text-font-colot-2;
				color: $text-font-colot-2;
			}
		}
	
	}
}
</style>
